package com.rongshu.sendmessage.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.base.BaseActivity;
import com.rongshu.sendmessage.base.MyApplication;
import com.rongshu.sendmessage.util.ClientPublicParamUtil;
import com.rongshu.sendmessage.util.StringUtils;
import com.rongshu.sendmessage.util.ToastUtils;
import com.rongshu.sendmessage.util.httpUtil.HYContent;
import com.rongshu.sendmessage.util.httpUtil.IModel;

import java.util.HashMap;
import java.util.Map;

import static com.rongshu.sendmessage.util.DialogUtil.hideProgress;
import static com.rongshu.sendmessage.util.DialogUtil.showProgress;

public class EditPasswordActivity extends BaseActivity {

    private Intent mIntent;
    private EditText et_phone,et_old_psw,et_new_psw,et_new_psw_2;
    private String userId;
    private String userPhone;
    private String isPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
        setContentView(R.layout.activity_edit_password);
        userId=MyApplication.mSettings.getString(HYContent.UserId,"");
        userPhone=MyApplication.mSettings.getString(HYContent.LOGIN_NAME,"");
        isPhone=MyApplication.mSettings.getString(HYContent.IsPhone,"");
        initView();
    }

    private void initView() {
        TextView title = findViewById(R.id.title);
        et_phone = findViewById(R.id.et_phone);
        et_old_psw = findViewById(R.id.et_old_psw);
        et_new_psw = findViewById(R.id.et_new_psw);
        et_new_psw_2 = findViewById(R.id.et_new_psw_2);
        TextView tv_name = findViewById(R.id.tv_name);
        String userName= MyApplication.mSettings.getString(HYContent.NickName,"");
        tv_name.setText(userName);
        LinearLayout titleLayLeft = findViewById(R.id.titleLayLeft);
        titleLayLeft.setOnClickListener(view->finish());
        title.setText("修改密码");
    }

    public void onClick(View view) {
        if (view.getId()==R.id.tv_wangji_mima){
            mIntent=new Intent(mContext,ForgetPasswordActivity.class);
            mIntent.putExtra("source", "setPassword");
            startActivity(mIntent);
        }else if (view.getId()==R.id.btn_submit){
            String account1 = et_phone.getText().toString();
            if (StringUtils.isEmpty(account1)) {
                ToastUtils.show(mContext, "手机号码不能为空！");
                return;
            }
            String password = et_new_psw.getText().toString();
            String password_two = et_new_psw_2.getText().toString();
            if (StringUtils.isEmpty(password) || StringUtils.isEmpty(password_two)) {
                ToastUtils.show(mContext, "密码不能为空！");
                return;
            }
            if (!password.equals(password_two)) {
                ToastUtils.show(mContext, "两次密码不一致");
                return;
            }
            if (password.length() < 6) {
                ToastUtils.show(mContext, "密码长度不能少于6位！");
                return;
            }

            String oldCode = et_old_psw.getText().toString();
            if (StringUtils.isEmpty(oldCode)) {
                ToastUtils.show(mContext, "旧密码不能为空！");
                return;
            }
            if (oldCode.length()<6) {
                ToastUtils.show(mContext, "密码至少六位！");
                return;
            }
            if ("true".equals(isPhone)){
                if (!et_phone.getText().toString().equals(userPhone)){
                    ToastUtils.show(mContext, "手机号码不正确！");
                    return;
                }

            }
            showProgress(mContext);
            //去验证code
            setPassword(account1, oldCode,password);
        }
    }
    //  替换新密码
    private void setPassword(String account,String oldCode, String password) {
        Map<String, String> param = new HashMap<>();
        param.put("userId",userId);
        param.put("newPassword",password);
        param.put("confirmNewPassword",password);
        param.put("oldPassword",oldCode);
        param.put("clientPublicParam", ClientPublicParamUtil.getPublicParams(mContext));
        String url= new HYContent().RESET_PASSWORD_URL;
        OkGo.<String>post(url).params(param).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                IModel.AjaxResult jr = new IModel.AjaxResult(response.body(),false);
                final int code = jr.getCode();
                if(code > 0) {
                    hideProgress();
                    ToastUtils.show(mContext,jr.getMsg());
                    finish();
                }else{
                    ToastUtils.show(mContext,jr.getMsg());
                    hideProgress();
                }
            }
            @Override
            public void onError(Response<String> response) {
                ToastUtils.show(mContext,"操作失败");
                hideProgress();
            }
        });
    }
}